package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        categoryPageQueryDTO.setPage((categoryPageQueryDTO.getPage()-1)*categoryPageQueryDTO.getPageSize());
        log.info("分页查询，参数：{}", categoryPageQueryDTO);
        Integer total = categoryMapper.count(categoryPageQueryDTO);
        List<Category> records = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(total,records);
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setStatus(1);
        category.setType(categoryDTO.getType());
        category.setName(categoryDTO.getName());
        category.setSort(categoryDTO.getSort());
        log.info("category:{}",category);
        categoryMapper.save(category);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.update(category);
    }

    @Override
    public void startorstop(Integer status, Long id) {
        categoryMapper.startorstop(status, id);
    }

    @Override
    public ArrayList<Category> list(int type) {
        if(type==1){
            return categoryMapper.list(type);
        }
        if(type==2){
            return categoryMapper.list(type);
        }
        return categoryMapper.list(1);
    }

    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    @Override
    public int getType(Long id) {
        return categoryMapper.getType(id);
    }

    @Override
    public ArrayList<Category> listAll() {
        return categoryMapper.listAll();
    }
}
